Comparing strings alphabetically

Greetings,

This seems like a very basic question to me, but I have not been able to find a clear answer in the dxl reference manual. I am wishing to compare two strings alphabetically. I was thinking about creating a function that uses the ASCII values of each character in the strings to compare them to each other letter by letter. I have read the section on string comparison in the reference manual, but it is not clear if it is comparing string length, string order, or some other arbitrary thing. Here is the text that I am finding confusing:

 

String relational operators can be used as shown in the following syntax:
string s1 == string s2
string s1 != string s2
string s1 < string s2l
string s1 > string s2
string s1 <= string s2
string s1 >= string s2
These operators return true if s1 is equal, not equal, less than, greater than, less than or equal to, or greater than or equal to s2. Case is significant.
Example
print ("aaaa" < "a" ) // prints "false"
print ("aaaa" > "a" ) // prints "true"
print ("aaaa" == "a" ) // prints "false"
print ("A" > "a" ) // prints "false"
print ("McDonald" < "Man" ) // prints "false"

The above text comes straight from the reference manual, but it doesn't state what one string being greater than another one means. If this is a good way to compare strings alphabetically, please let me know!

Thanks!


CMusicFan - Wed Mar 26 10:42:24 EDT 2014

Re: Comparing strings alphabetically
Tony_Goodman - Wed Mar 26 11:07:11 EDT 2014

Case is significant, but your examples do not demonstrate this. Try

print ("B" < "a")  // prints true

So the comparison is NOT alphabetical, it is according to ASCII value.

If you want to compare alphabetically, you will need to take the case of the string out of the equation by using cistrcmp(s1, s2).

An alternative might be to use upper(s) or lower(s2)

Hope that helps

Re: Comparing strings alphabetically
CMusicFan - Wed Mar 26 11:11:14 EDT 2014

Tony_Goodman - Wed Mar 26 11:07:11 EDT 2014

Case is significant, but your examples do not demonstrate this. Try

print ("B" < "a")  // prints true

So the comparison is NOT alphabetical, it is according to ASCII value.

If you want to compare alphabetically, you will need to take the case of the string out of the equation by using cistrcmp(s1, s2).

An alternative might be to use upper(s) or lower(s2)

Hope that helps

I think it does. What I was really wanting to know was what "greater than" actually means in this context. All of the above cases could refer to string length and still be true. I was unsure if "greater than" meant greater in ascii value, or greater in string length, or greater in some other aspect.

Thanks!

Re: Comparing strings alphabetically
Tony_Goodman - Wed Mar 26 11:25:14 EDT 2014

CMusicFan - Wed Mar 26 11:11:14 EDT 2014

I think it does. What I was really wanting to know was what "greater than" actually means in this context. All of the above cases could refer to string length and still be true. I was unsure if "greater than" meant greater in ascii value, or greater in string length, or greater in some other aspect.

Thanks!

I see, for that we have the length function.

 

print (length("AAA") > length("BB"))   // prints true

Re: Comparing strings alphabetically
Tony_Goodman - Wed Mar 26 11:25:51 EDT 2014

CMusicFan - Wed Mar 26 11:11:14 EDT 2014

I think it does. What I was really wanting to know was what "greater than" actually means in this context. All of the above cases could refer to string length and still be true. I was unsure if "greater than" meant greater in ascii value, or greater in string length, or greater in some other aspect.

Thanks!

I see, for that we have the length function.

 

print (length("AAA") > length("BB"))   // prints true